package io.renren.modules.dishes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.fasterxml.jackson.core.JsonProcessingException;
import io.renren.common.utils.MinioUtilS;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.dishes.dao.spuDao;
import io.renren.modules.dishes.entity.CommoditySpuEntity;
import io.renren.modules.dishes.entity.SpuVo;
import io.renren.modules.dishes.service.spuService;
import io.renren.modules.merchant.service.impl.MerchantServiceImpl;
import io.renren.modules.sys.entity.SysRoleEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import static io.renren.common.utils.ShiroUtils.getUserId;

@Service
public class  spuServiceImpl extends ServiceImpl<spuDao, CommoditySpuEntity> implements spuService {

    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;

    @Autowired
    private MerchantServiceImpl merchantService;

    @Autowired
    private spuDao spuDao;

    @Autowired
    private ClassficationServiceImpl classficationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String merchantId = (String)params.get("merchantId");
        String classifyId =  (String) params.get("classifyId");
        String spuName = (String) params.get("spuName");
        IPage<CommoditySpuEntity> page;

        if (classifyId.equals("0")){
             page = this.page(
                    new Query<CommoditySpuEntity>().getPage(params),
                    new QueryWrapper<CommoditySpuEntity>()
                            .eq(StringUtils.isNotBlank(merchantId),"merchant_id",merchantId)
                            .like(StringUtils.isNotBlank(spuName),"spu_name",spuName));
        }else {
            page = this.page(
                    new Query<CommoditySpuEntity>().getPage(params),
                    new QueryWrapper<CommoditySpuEntity>()
                            .eq(StringUtils.isNotBlank(merchantId),"merchant_id",merchantId)
                            .eq(StringUtils.isNotBlank(classifyId),"classify_id",classifyId)
                            .like(StringUtils.isNotBlank(spuName),"spu_name",spuName));
        }

        return new PageUtils(page);
    }

    @Override
    public Boolean saveSpuVO(SpuVo spuVo) throws JsonProcessingException {
        CommoditySpuEntity commoditySpuEntity = new CommoditySpuEntity();

        String uuid = UUID.randomUUID().toString().replace("-", ""); // 移除短横线，得到32位
        String shortId = uuid.substring(0, 32); // 截取前32位
        commoditySpuEntity.setSpuId(shortId);
        Long userId = getUserId();
        String merchantId = merchantService.getMerchantByUserId(userId).getMerchantId();
        commoditySpuEntity.setMerchantId(merchantId);

        //vo转
        commoditySpuEntity.setSpuName(spuVo.getSpuName());
        commoditySpuEntity.setSpuImage(spuVo.getSpuImage());
        commoditySpuEntity.setRecommendReason(spuVo.getRecommendReason());
        commoditySpuEntity.setClassifyId(spuVo.getClassifyId());

        commoditySpuEntity.setClassifyName(classficationService.getClassficationById(spuVo.getClassifyId()).getClassifyName());

        commoditySpuEntity.setDishesDetail(spuVo.getDishesDetail().toJsonString());
        commoditySpuEntity.setNutritionalContent(spuVo.getNutritionalContent().toJsonString());
        commoditySpuEntity.setSalesVolume(0);
        return spuDao.saveSpu(commoditySpuEntity);
    }

    @Override
    public SpuVo getVO(String spuId) throws IOException {
        CommoditySpuEntity entity = spuDao.selectById(spuId);

        SpuVo spuVo=new SpuVo();

        spuVo.setSpuImage(entity.getSpuImage());
        spuVo.setSpuName(entity.getSpuName());
        spuVo.setRecommendReason(entity.getRecommendReason());
        spuVo.setClassifyId(entity.getClassifyId());
        spuVo.setSpuId(entity.getSpuId());

        spuVo.setDishesDetail(SpuVo.fromJsonStringToDishesDetail(entity.getDishesDetail()));
        spuVo.setNutritionalContent(SpuVo.fromJsonStringToNutritionalContent(entity.getNutritionalContent()));

        return spuVo;
    }

    @Override
    public Boolean updateSpuVO(SpuVo spuVo) throws JsonProcessingException {
        CommoditySpuEntity commoditySpuEntity = new CommoditySpuEntity();
        commoditySpuEntity.setSpuId(spuVo.getSpuId());

        CommoditySpuEntity byId = this.getById(spuVo.getSpuId());
        //vo转

        commoditySpuEntity.setSpuImage(spuVo.getSpuImage());
        if (!byId.getSpuImage().equals(spuVo.getSpuImage())){
            minioUtilS.removeFile(bucketName,byId.getSpuImage());
        }

        commoditySpuEntity.setSpuName(spuVo.getSpuName());
        commoditySpuEntity.setRecommendReason(spuVo.getRecommendReason());
        commoditySpuEntity.setClassifyId(spuVo.getClassifyId());
        commoditySpuEntity.setClassifyName(classficationService.getClassficationById(spuVo.getClassifyId()).getClassifyName());

        commoditySpuEntity.setDishesDetail(spuVo.getDishesDetail().toJsonString());
        commoditySpuEntity.setNutritionalContent(spuVo.getNutritionalContent().toJsonString());
        return this.updateById(commoditySpuEntity);
    }

    @Override
    public Integer deleteBatch(String[] spuIds) {
        for (String spuId : spuIds) {
            CommoditySpuEntity entity = spuDao.selectById(spuId);
            minioUtilS.removeFile(bucketName,entity.getSpuImage());
        }
        return spuDao.deleteBatchIds(Arrays.asList(spuIds));
    }
}
